<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <PropertyPageSchema
      Include="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml" />
    <AvailableItemName
      Include="YASM">
      <Targets>_YASM</Targets>
    </AvailableItemName>
  </ItemGroup>

  <PropertyGroup>
    <ComputeLinkInputsTargets>
      $(ComputeLinkInputsTargets);
      ComputeYASMOutput;
    </ComputeLinkInputsTargets>
    <ComputeLibInputsTargets>
      $(ComputeLibInputsTargets);
      ComputeYASMOutput;
    </ComputeLibInputsTargets>
  </PropertyGroup>

  <UsingTask
    TaskName="YASM"
    TaskFactory="XamlTaskFactory"
    AssemblyName="Microsoft.Build.Tasks.v4.0">
    <Task>$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml</Task>
  </UsingTask>

  <Target Name="_WriteYASMTlogs"
          Condition="'@(YASM)' != '' and '@(SelectedFiles)' == ''">

    <ItemGroup>
      <_YASMReadTlog 
        Include="^%(YASM.FullPath);%(YASM.AdditionalDependencies)"
        Condition="'%(YASM.ExcludedFromBuild)' != 'true' and '%(YASM.ObjectFile)' != ''"/>
      <_YASMWriteTlog 
        Include="^%(YASM.FullPath);$([MSBuild]::NormalizePath('$(MSBuildProjectDirectory)', '%(YASM.ObjectFile)'))" 
        Condition="'%(YASM.ExcludedFromBuild)' != 'true' and '%(YASM.ObjectFile)' != ''"/>
    </ItemGroup>

    <WriteLinesToFile
      Condition="'@(_YASMReadTlog)' != ''"
      File="$(TLogLocation)YASM.read.1u.tlog"
      Lines="@(_YASMReadTlog->MetaData('Identity')->ToUpperInvariant());"
      Overwrite="true"
      Encoding="Unicode"/>

    <WriteLinesToFile
      Condition="'@(_YASMWriteTlog)' != ''"
      File="$(TLogLocation)YASM.write.1u.tlog"
      Lines="@(_YASMWriteTlog->MetaData('Identity')->ToUpperInvariant());"
      Overwrite="true"
      Encoding="Unicode"/>

    <ItemGroup>
      <_YASMReadTlog Remove="@(_YASMReadTlog)" />
      <_YASMWriteTlog Remove="@(_YASMWriteTlog)" />
    </ItemGroup>

  </Target>
 
  <Target
    Name="_YASM"
    BeforeTargets="$(YASMBeforeTargets)"
    AfterTargets="$(YASMAfterTargets)"
    Condition="'@(YASM)' != ''"
    Inputs="%(YASM.Identity);%(YASM.AdditionalDependencies);$(MSBuildProjectFile)"
    Outputs="%(YASM.ObjectFile)"
    DependsOnTargets="PrepareForBuild;_WriteYasmTlogs;_SelectedFiles;$(YASMDependsOn);">

    <ItemGroup Condition="'@(SelectedFiles)' != ''">
      <YASM Remove="@(YASM)" Condition="'%(Identity)' != '@(SelectedFiles)'" />
    </ItemGroup>

    <Message
      Importance="High"
      Text="%(YASM.ExecutionDescription)" />

    <YASM
      Condition="'@(YASM)' != '' and '%(YASM.ExcludedFromBuild)' != 'true'"
      CommandLineTemplate="%(YASM.CommandLineTemplate)"
      Debug="%(YASM.Debug)"
      PreIncludeFile="%(YASM.PreIncludeFile)"
      IncludePaths="%(YASM.IncludePaths)"
      Defines="%(YASM.Defines)"
      UnDefines="%(YASM.UnDefines)"
      ObjectFile="%(YASM.ObjectFile)"
      ListFile="%(YASM.ListFile)"
      MapFile="%(YASM.MapFile)"
      ErrorFile="%(YASM.ErrorFile)"
      SymbolPrefix="%(YASM.SymbolPrefix)"
      SymbolSuffix="%(YASM.SymbolSuffix)"
      PreProc="%(YASM.PreProc)"
      Parser="%(YASM.Parser)"
      AdditionalOptions="%(YASM.AdditionalOptions)"
      Inputs="%(YASM.Identity)" />
  </Target>
  
  <Target
    Name="ComputeYASMOutput"
    Condition="'@(YASM)' != ''">
    <ItemGroup>
      <Link Include="@(YASM->Metadata('ObjectFile')->Distinct()->ClearMetadata())" Condition="'%(YASM.ExcludedFromBuild)' != 'true'"/>
      <Lib Include="@(YASM->Metadata('ObjectFile')->Distinct()->ClearMetadata())" Condition="'%(YASM.ExcludedFromBuild)' != 'true'"/>
    </ItemGroup>
  </Target>

</Project>